<?php

/**
 * This is the model class for table "lesson".
 *
 * The followings are the available columns in table 'lesson':
 * @property integer $lesson_id
 * @property integer $user_id
 * @property string $lesson_name
 * @property string $lesson_detail
 * @property string $lesson_date
 * @property string $password
 */
class Lesson extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Lesson the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'lesson';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_id, lesson_name, lesson_detail, lesson_date, password', 'required'),
			array('user_id', 'numerical', 'integerOnly'=>true),
			array('lesson_name, lesson_detail', 'length', 'max'=>64),
			array('password', 'length', 'max'=>32),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('lesson_id, user_id, lesson_name, lesson_detail, lesson_date, password', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'chapters'=>array(self::HAS_MANY, 'Chapter', 'lesson_id'),
			'user'=>array(self::BELONGS_TO, 'User', 'user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'lesson_id' => 'Lesson',
			'user_id' => 'User',
			'lesson_name' => 'Lesson Name',
			'lesson_detail' => 'Lesson Detail',
			'lesson_date' => 'Lesson Date',
			'password' => 'Password',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('lesson_id',$this->lesson_id);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('lesson_name',$this->lesson_name,true);
		$criteria->compare('lesson_detail',$this->lesson_detail,true);
		$criteria->compare('lesson_date',$this->lesson_date,true);
		$criteria->compare('password',$this->password,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	// ตรวจสอบว่าเป็น owner หรือไม่
	public function isOwner()
	{
		if($this->user_id === Yii::app()->user->id){
			return true;
		}
		return false;
	}
	
	// ลบ chapter และ Rememberpassword ทั้งหมดที่เกี่ยวข้องกับ Lesson ที่จะลบ
	public function delete()
	{
		$chapters = Chapter::model()->findAllByAttributes(array('lesson_id'=>$this->lesson_id));
		$rmps = RememberPassword::model()->findAllByAttributes(array('lesson_id'=>$this->lesson_id));
		$docs = Document::model()->findAllByAttributes(array('lesson_id'=>$this->lesson_id));
		$images = Image::model()->findAllByAttributes(array('lesson_id'=>$this->lesson_id));
		$videos = Video::model()->findAllByAttributes(array('lesson_id'=>$this->lesson_id));
		
		if($chapters !== null){
			foreach($chapters as $key => $chapter){
				$chapter->delete();
			}
		}
		if($rmps !== null){
			foreach($rmps as $key => $rmp){
				$rmp->delete();
			}
		}
		if($docs !== null){
			foreach($docs as $key => $doc){
				$doc->delete();
			}
		}
		if($images !== null){
			foreach($images as $key => $image){
				$image->delete();
			}
		}
		if($videos !== null){
			foreach($videos as $key => $video){
				$video->delete();
			}
		}
		
		return parent::delete();
	}
}